//
//  BCOptionVC.m
//  BangcaiDriver
//
//  Created by lsy on 16/8/22.
//  Copyright © 2016年 BC. All rights reserved.
//

#import "BCOptionVC.h"
#import "BCSettingCollectionViewCell.h"
#import "BCFindPssVC.h"

@interface BCOptionVC () <UICollectionViewDelegate,UICollectionViewDataSource>
//主视图
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) UIButton *logoutButton;
@end

@implementation BCOptionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self p_initNavgationBar];
    [self p_initLogoutButton];
    [self p_initBackgroundLogo];
    [self p_initCollectionView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = NO;
    
    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"设置";
    self.view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.000];
}

-(void)p_initLogoutButton {
    self.logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.logoutButton.layer.cornerRadius = 3.f;
    self.logoutButton.backgroundColor = BangcaiOrange;
    [self.logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.logoutButton setTitle:@"退出" forState:UIControlStateNormal];
    CGFloat logoutButton_x = 14.f;
    CGFloat logoutButton_h = 53.f;
    self.logoutButton.frame = CGRectMake(logoutButton_x, SCREEN_HEIGHT-12.f-logoutButton_h-self.navigationController.navigationBar.frame.size.height-20.f, SCREEN_WIDTH-(logoutButton_x*2), logoutButton_h);
    [self.logoutButton addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_logoutButton];
}

-(void)p_initBackgroundLogo {
    UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LOGO"]];
    logoImageView.frame = CGRectMake((SCREEN_WIDTH-82.f)/2, SCREEN_HEIGHT-12.f-_logoutButton.frame.size.height-14.f-155.f-self.navigationController.navigationBar.frame.size.height-20.f, 82.f, 155.f);
    logoImageView.backgroundColor = [UIColor clearColor];
    logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:logoImageView];
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 3, SCREEN_WIDTH, SCREEN_HEIGHT-_logoutButton.frame.size.height-12.f-3.f) collectionViewLayout:flowLayout];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 12.f, 0);
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainCollectionView];
}

#pragma mark - Private Action
-(void)logoutAction:(UIButton *)sender {
    NSLog(@"logout action");
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"MineSettingCell";
    BCSettingCollectionViewCell *cell = [BCSettingCollectionViewCell cellWithCollectionView:collectionView];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.titleLable.text = @"修改密码";
        cell.iconImageView.image = [UIImage imageNamed:cell.titleLable.text];
    }
    else {
        cell.titleLable.text = @"更新";
        cell.iconImageView.image = [UIImage imageNamed:cell.titleLable.text];
    }
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(self.view.frame.size.width, 60.f);
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.f;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        BCFindPssVC *nextViewController = [[BCFindPssVC alloc] init];
        [self.navigationController pushViewController:nextViewController animated:YES];
    }
}
@end
